无 15:15:41
<!-- 车型添加 -->
<template>
  <div>
    <el-dialog
      width="40%"
      :visible="showDialog"
      :title="showTitle"
      @close="closeDialog"
    >
      <el-form
        ref="addform"
        :model="addFormData"
        :rules="rules"
        label-width="110px"
        style="align:center"
      >
        <el-form-item style="font-weight:400" label="车辆类型" prop="name">
          <el-input
            v-model="addFormData.name"
            maxlength="15"
            placeholder="请输入车辆类型"
          />
        </el-form-item>
        <el-form-item label="应载重量" prop="allowableLoad">
          <el-input
            v-model="addFormData.allowableLoad"
            maxlength="10"
            placeholder="请输入车型载重"
          >
            <i
              slot="suffix"
              style="font-style:normal;color:black;margin-right:10px"
              >吨</i
            >
          </el-input>
        </el-form-item>
        <el-form-item label="应载体积" prop="allowableVolume">
          <el-input
            v-model="addFormData.allowableVolume"
            maxlength="10"
            placeholder="请输入车型体积"
          >
            <i
              slot="suffix"
              style="font-style:normal;color:black;margin-right:10px"
              >立方</i
            >
          </el-input>
        </el-form-item>
        <el-form-item label="长" prop="measureLong">
          <el-input v-model="addFormData.measureLong">
            <i
              slot="suffix"
              style="font-style:normal;color:black;margin-right:10px"
              >米</i
            >
          </el-input>
        </el-form-item>
        <el-form-item label="宽" prop="measureWidth">
          <el-input v-model="addFormData.measureWidth">
            <i
              slot="suffix"
              style="font-style:normal;color:black;margin-right:10px"
              >米</i
            >
          </el-input>
        </el-form-item>
        <el-form-item label="高" prop="measureHigh">
          <el-input v-model="addFormData.measureHigh">
            <!-- <i
              slot="prefix"
              style="font-style:normal;color:black;margin-left:10px"
            ></i> -->
            <i
              slot="suffix"
              style="font-style:normal;color:black;margin-right:10px"
              >米</i
            >
          </el-input>
        </el-form-item>
        <el-form-item>
          <el-row type="flex" justify="center" style="padding-bottom:25px">
            <el-col :span="12">
              <el-button type="danger" @click="btnOk">确定</el-button>
              <el-button @click="closeDialog">取消</el-button>
            </el-col>
          </el-row>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>
<script>
import {
  add as addCarModel,
  detail as getDetail,
  update as updateCarModel
} from '@/api/transit'
export default {
  name: 'CarModelsAdd',
  props: {
    showDialog: {
      type: Boolean,
      default: false
    },
    currentId: {
      type: String,
      default: ''
    }
  },
  data() {
    return {
      msg: '',
      addFormData: {
        allowableLoad: '',
        allowableVolume: '',
        goodsTypes: [],
        id: null,
        measureHigh: '0.0',
        measureLong: '0.0',
        measureWidth: '0.0',
        name: '',
        num: 0
      },
      // 表单验证
      rules: {
        name: [
          { required: true, message: '车型名称不能为空', trigger: 'blur' }
        ],
        allowableLoad: [
          { required: true, message: '车载重量不能为空', trigger: 'blur' },
          {
            pattern: /^([1-9][0-9]{0,1}|100)$/,
            message: '只可输入1-100之间的整数',
            trigger: 'blur'
          }
        ],
        allowableVolume: [
          { required: true, message: '车载体积不能为空', trigger: 'blur' },
          {
            pattern: /^([1-9][0-9]{0,1}|100)$/,
            message: '只可输入1-100之间的整数',
            trigger: 'blur'
          }
        ]
      }
    }
  },
  computed: {
    showTitle() {
      return this.currentId ? '编辑车型' : '新增车型'
    }
  },
  created() {
    console.log('currentId', this.currentId)
    // this.addCarModel()
    // this.getDetail()
  },
  methods: {
    closeDialog() {
      this.$refs.addform.resetFields()
      this.addFormData = {
        allowableLoad: '',
        allowableVolume: '',
        goodsTypes: [],
        id: null,
        measureHigh: '0.0',
        measureLong: '0.0',
        measureWidth: '0.0',
        name: '',
        num: 0
      }
      this.$emit('changeCurrentId', null)

      this.$emit('update:showDialog', false)
    },
    btnOk() {
      this.$refs.addform.validate(async isOk => {
        if (isOk) {
          if (this.currentId) {
            this.addFormData.id = this.currentId
            await updateCarModel(this.currentId, this.addFormData)
            this.$message.success('编辑车型成功')
          } else {
            try {
              const res = await addCarModel(this.addFormData)
              console.log(res)
              if (res.code === 200) {
                this.$message.success(
                  this.currentId ? '编辑车型成功' : '新增车型成功'
                )
              } else {
                this.$message(res.msg)
              }
            } catch (error) {
              console.log('error', error)
              this.$message.error('新增失败')
            }
          }
          this.closeDialog()
          this.$emit('updateFromDate')
        }
      })
    },
    async getDetail() {
      const res = await getDetail(this.currentId)
      console.log('res', res)
      this.addFormData = res.data
    }
  }
}
</script>
<style scoped>
::v-deep .el-form-item__label {
  font-weight: normal;
}
</style>
